<?php
	require("libraries/student.php");
	class Trains extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->helper("form");
			$this->load->library("form_validation");
			$this->load->model("model_train");
			$this->load->library("string");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/trains/index/';
			$config['total_rows'] = $this->model_train->count_all();
			$data['title'] = "Manage trains";
			$data['template'] = "trains/list_train";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 6;
			$data['listall']= $this->model_train->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function active(){
			$id   	= $this->uri->segment(4);
			$status	= $this->uri->segment(5);
			$db = array(
				"train_status" => $status 
			);
			$this->model_train->update_status($db,$id);
			redirect(base_url()."admin/trains");
		}
		public function add(){
		  	$data['title'] = "Add train";
			$data['template'] = "trains/add_train";
			$data['data'] = "";
			$data['act'] = 6;
			if($this->input->post("ok") != ""){
				$url = str_replace(' ','-',strtolower($this->string->replace($this->input->post('train_name'))));
				$value = array(
						"train_name" => $this->input->post("train_name"),
						"train_code" => $this->input->post("train_code"),
						"train_rewrite" => $url,
						"train_info" => $this->input->post("train_info"),
                        "train_overview" => $this->input->post("train_overview"),
						"train_amenities" => $this->input->post("train_amenities"),
						"train_schedule" => $this->input->post("train_schedule"),
						"train_pros" => $this->input->post("train_pros"),
						"train_keys" => $this->input->post("train_keys"),
						"train_descrip" => $this->input->post("train_descrip"),
						"train_hot" => $this->input->post("train_hot"),
						"train_order" => $this->input->post("train_order"),
						"train_status" => "1"
				);
				if($_FILES['image']['name'] != NULL){
					$config['upload_path']   = './uploads/trains';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '1500';
					$config['max_width']  = '1000';
					$config['max_height']  = '1000';
					$config['encrypt_name'] = TRUE;
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("image")){
						$data = array('error' => $this->upload->display_errors());
						$data['title'] = "Add train";
						$data['template'] = "trains/add_train";
						$data['data'] = "";
						$data['act'] = 6;
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$value['train_image'] = $data['file_name'];
						$this->createThumbnail("uploads/trains",$value['train_image'],160,110);
					}
				}
				$img = array();
				$now = "";
				if($_FILES['fullimg']['name'][0] != NULL){
					for($i=0;$i<count($_FILES['fullimg']['name']);$i++){
						$name = $_FILES['fullimg']['name'][$i];
						$type = $_FILES['fullimg']['type'][$i];
						$size = $_FILES['fullimg']['size'][$i];
						if($type == "image/jpeg" || $type == "image/png" || $type == "image/gif"){
							if($size > 1048576){
								die("File size not larger than 1mb");
							}else{
								$now = date("dmYhis");
								if(!file_exists("uploads/trains/".$now)){
									mkdir("uploads/trains/".$now,0777);
								}
								$value['train_folderimg'] = $now;
								move_uploaded_file($_FILES['fullimg']['tmp_name'][$i],"uploads/trains/".$now."/".$_FILES['fullimg']['name'][$i]);
								$img[] = $_FILES['fullimg']['name'][$i];
							}
						}else{
							die("Invalid file type : ".$type);
						}
						//echo $name." - ".$type." - ".$size."<br />";
					}
					$value['train_fullimg'] = serialize($img);
				}
				$this->model_train->add($value);
				redirect(base_url()."admin/trains");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['title']= "Edit train";
			$data['template'] = "trains/edit_train";
			$data['data'] = "";
			$data['act'] = 6;
			$data['getdata'] = $this->model_train->getdata($id);
			if($this->input->post("ok") != ""){
				$url = str_replace(' ','-',strtolower($this->string->replace($this->input->post('train_name'))));
				$value = array(
					"train_name" => $this->input->post("train_name"),
					"train_code" => $this->input->post("train_code"),
					"train_rewrite" => $url,
					"train_info" => $this->input->post("train_info"),
					"train_overview" => $this->input->post("train_overview"),
					"train_amenities" => $this->input->post("train_amenities"),
					"train_schedule" => $this->input->post("train_schedule"),
					"train_pros" => $this->input->post("train_pros"),
					"train_keys" => $this->input->post("train_keys"),
					"train_descrip" => $this->input->post("train_descrip"),
					"train_hot" => $this->input->post("train_hot"),
					"train_order" => $this->input->post("train_order")
				);
				if($_FILES['image']['name'] != NULL){
					$config['upload_path']   = './uploads/trains';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '1500';
					$config['max_width']  = '1000';
					$config['max_height']  = '1000';
					$config['encrypt_name'] = TRUE;
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("image")){
						$data = array('error' => $this->upload->display_errors());
						$data['title'] = "Add train";
						$data['template'] = "trains/add_train";
						$data['data'] = "";
						$data['act'] = 6;
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$value['train_image'] = $data['file_name'];
						$this->createThumbnail("uploads/trains",$value['train_image'],160,110);
					}
				}
				$this->model_train->update($value,$id);
				redirect(base_url()."admin/trains");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$this->model_train->del($id);
			redirect(base_url()."admin/trains/index");
		}
		public function createThumbnail($parth,$fileName,$width,$height){
	        $this->load->library('image_lib');
	        $config['image_library'] = 'gd2';
	        $config['source_image'] = $parth.'/'.$fileName;
	        $config['new_image'] = $parth.'/thumb/'.$fileName;
	        $config['create_thumb'] = TRUE;
	        $config['maintain_ratio'] = TRUE;
	        $config['thumb_marker'] = FALSE;
	        $config['width'] = $width;
	        $config['height'] = $height;
	        $this->image_lib->initialize($config);
	        $this->image_lib->resize();
	        $this->image_lib->clear();
	    }
	}